package com.myh.cc.coom.singer.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.myh.cc.coom.singer.bean.TypeBean
import com.myh.cc.coom.singer.viewmodel.SingerModel
import com.myh.cc.databinding.ItemRadioButtonBinding

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/15.   ：
 */
class TypeAdapter(bean: List<TypeBean>,model: SingerModel): RecyclerView.Adapter<TypeAdapter.VH>(),View.OnClickListener {

    private var selectedPosition: Int = 0
    val bean = bean
    val model = model
    // 为了两个 adapter 复用
    var keyType = "all_all"
    var keyAbc  = "all"

    override fun onBindViewHolder(holder: VH, position: Int) {

        holder.binding.itemRb.isChecked= (position==selectedPosition)
        holder.binding.itemRb.text = bean[position].name
        holder.binding.itemRb.tag = position
        holder.binding.root.setOnClickListener(this)

    }

    override fun getItemCount(): Int {

        return bean.size
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        return VH.create(LayoutInflater.from(parent.context),parent)
    }

    override fun onClick(p0: View) {

        selectedPosition = p0.tag  as Int

        notifyDataSetChanged()

        keyAbc = bean[selectedPosition].key
        keyType = bean[selectedPosition].key
        model.getSinger(bean[selectedPosition].key,"")

    }



    class VH(binding: ItemRadioButtonBinding): RecyclerView.ViewHolder(binding.root){

        val binding = binding

        companion object {
            fun  create(inflater: LayoutInflater,viewGroup: ViewGroup): VH{
                val  binder = ItemRadioButtonBinding.inflate(inflater,viewGroup,false)
                return VH(binder)

            }
        }

    }
}